app.register.controller('registerCtrl',['$scope','$location', '$http','$interval','$timeout','$cookies',function ($scope,$location,$http,$interval,$timeout,$cookies) {
    //数据
    $scope.user = {};
    $scope.verityText = '获取验证码';
    $scope.user = {
        tellTop: '86-'
    }

    //获取验证码
    var isNoMobleClick = true;
    $scope.mobileCode = function () {
        console.log($scope.user.phone);
        //验证手机号
        if(!$scope.user.phone){
            $scope.popShow('手机号不能为空！');
            return false;
        }
        if(!$config.rex.gattel.test($scope.user.phone)){
            $scope.popShow('手机号格式不正确！');
            return false;
        }

        if(!isNoMobleClick) return;
        isNoMobleClick = false;
        //请求短信验证码
        $http({
            method:'POST',
            url:$config.host+'/api/users/GetMobileVerificationCode',
            params:{
                strMobile : $scope.user.tellTop+$scope.user.phone,
                strSmsType : 'RegisterCode'
            }
        }).success(function (json) {
            // console.log(JSON.stringify(json));
            if(json.nFlag==1){
                Countdown();
            }else{
                console.log('获取验证码失败');
                isNoMobleClick = true;
                $scope.popShow(json.strMessage);
            }
        })

        // console.log('点击');
        // $timeout(function () {
        //     Countdown();
        // },1000);
    }

    //短信倒计时
    function Countdown() {
        var num = 60;
        $scope.verityText = num+'s';
        var time = $interval(function () {
            if(num>0){
                $scope.verityText = num+'秒';
                num--;
            }else{
                $interval.cancel(time);
                $scope.verityText = '获取验证码';
                isNoMobleClick = true;
            }
        },1000);
    }

    //提交
    $scope.submitRegister = function (){
        //验证
        if(!formVerity())return;

        //提交注册
        $http({
            method:'POST',
            url:$config.host+'/api/users/register',
            params:{
                strUserName : $scope.user.name,
                strPassword : $scope.user.pwd,
                nMobilePre : $scope.user.tellTop,
                strMobile : $scope.user.phone,
                nCode : $scope.user.code,
                StrUserType : 'CommonUser'
            }
        }).success(function (json) {
            // console.log(JSON.stringify(json));
            if(json.nFlag==1){
                console.log('注册成功');

                var mobilePre = !json.data.userInfo.mobilePre?'86-':json.data.userInfo.mobilePre;
                var mobile =json.data.userInfo.mobile;
                console.log(mobilePre,mobile);
                //再次请起登录
                $http({
                    method:'POST',
                    url:$config.host+'/api/users/getSalt',
                    params:{
                        uName : mobilePre+mobile
                    }
                }).success(function (json) {
                    if(json.nFlag==1){
                        $http({
                            method:'POST',
                            url:$config.host+'/api/users/Login',
                            params:{
                                strPassword : md5(json.data.strSalt+$scope.user.pwd),
                                nUserId : json.data.nUserId,
                                t : new Date().getTime(),
                                strTokenId : json.data.strTokenId,
                                strHash : md5(json.data.strTokenId+json.data.nUserId)

                            }
                        }).success(function (data) {
                            // console.log(JSON.stringify(json));
                            if(data.nFlag==1){
                                $cookies.strTokenId = data.data.strTokenId;
                                $cookies.nUserId = data.data.nUserId;

                                if($cookies.nEventId){
                                    $location.path('/detail/'+$cookies.nEventId);
                                }else{
                                    $location.path('/home');
                                }
                            }else{
                                $scope.popShow(data.strMessage);
                            }
                        })
                    }else {
                        $scope.popShow(json.strMessage);
                    }
                });




            }else{
                console.log('注册失败')
                $scope.popShow(json.strMessage);
            }
        })


    }

    //表单验证
    function formVerity(){
        if(!$scope.user.name){
            $scope.popShow('用户名不能为空！');
            return false;
        }
        if(!$scope.user.pwd){
            $scope.popShow('密码不能为空！');
            return false;
        }
        if(!$scope.user.phone){
            $scope.popShow('手机号不能为空！');
            return false;
        }
        if(!$config.rex.gattel.test($scope.user.phone)){
            $scope.popShow('手机号格式不正确！');
            return false;
        }
        if(!$scope.user.code){
            $scope.popShow('验证码不能为空！');
            return false;
        }
        return true;
    }

}]);